from django.shortcuts import render
from rest_framework.views import APIView
from users.models import User
from rest_framework.response import Response
class CheckMobile(APIView): # 判断手机号是否重复
    def get(self,request,mobile):
        rest = User.objects.filter(mobile = mobile).count()
        if rest>0:
            return Response({'msg':"手机号已经存在"})
        return Response({"bc":'200'})

class CheckUsername(APIView): # 判断用户名是否存在
    def get(self,request,name):
        rest = User.objects.filter(username = name).count()
        if rest >0:
            return Response({"msg":"用户已经存在"})
        return Response({"bc":"200"})

class UserRegister(APIView):
    # 注册账号
    def post(self,request):
        user_name = request.data.get('userName')
        pwd = request.data.get('pwd')
        mobile = request.data.get('mobile')
        agree = request.data.get('agree')
        if not all([user_name,pwd,mobile,agree]):
            return Response({"msg":"请输入完整"})

        if not agree:
            return Response({"msg":"请勾选协议"})

        User.objects.create_user(username=user_name,password=pwd,mobile = mobile,agree_rule =agree)
        return Response({'msg':"注册成功"})
